/*
 * 文件名：FileUploadController.java
 * 版权：MoonBox 
 * 修改人：Peter
 * 修改时间：2018年9月30日
 * 修改内容：新增
 */
package com.renzhebiao.lkang.commonController;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.renzhebiao.lkang.utils.EmptyTools;
import com.renzhebiao.lkang.utils.FileUtils;

import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;

/**
 * <p>
 * 图片上传
 * 
 * @author Peter
 * @since 1.0.0
 */
@Log4j
@RestController
public class FileUploadController {

    @Value("${img.upload-path}")
    private String localPath;
    private String head = "http://47.107.84.101/";

    /**
     * @param file 上传的文件
     * @return
     */
    @ApiOperation(value = "图片上传", notes = "图片上传", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/upload/img")
    public String upload(@RequestParam("file") MultipartFile file) {
        // 1定义要上传文件 的存放路径
        // String localPath = "D:/image";
        // 2获得文件名字
        String fileName = file.getOriginalFilename();
        // 2上传失败提示
        String warning = "";
        String imgPath = FileUtils.upload(file, localPath, fileName);
        if (EmptyTools.isNotNullOrEmpty(imgPath)) {
            // 上传成功
            warning = "上传成功";

        } else {
            warning = "上传失败";
        }
        log.debug(warning);
        // System.out.println(warning);
        return head + imgPath;
    }

}
